package com.lxmes.sales.service.impl;

import java.util.List;

import com.lxmes.base.domain.NcSupplier;
import com.lxmes.base.mapper.NcSupplierMapper;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.sales.domain.JcIteminfoPo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.sales.mapper.NcPurchaseTemporaryPlanMapper;
import com.lxmes.sales.domain.NcPurchaseTemporaryPlan;
import com.lxmes.sales.service.INcPurchaseTemporaryPlanService;

/**
 * 采购临时计划Service业务层处理
 *
 * @author lx
 * @date 2023-06-06
 */
@Service
public class NcPurchaseTemporaryPlanServiceImpl implements INcPurchaseTemporaryPlanService
{
    @Autowired
    private NcPurchaseTemporaryPlanMapper ncPurchaseTemporaryPlanMapper;
    @Autowired
    private NcSupplierMapper ncSupplierMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询采购临时计划
     *
     * @param id 采购临时计划主键
     * @return 采购临时计划
     */
    @Override
    public NcPurchaseTemporaryPlan selectNcPurchaseTemporaryPlanById(Long id)
    {
        return ncPurchaseTemporaryPlanMapper.selectNcPurchaseTemporaryPlanById(id);
    }

    /**
     * 查询采购临时计划列表
     *
     * @param ncPurchaseTemporaryPlan 采购临时计划
     * @return 采购临时计划
     */
    @Override
    public List<NcPurchaseTemporaryPlan> selectNcPurchaseTemporaryPlanList(NcPurchaseTemporaryPlan ncPurchaseTemporaryPlan)
    {
        return ncPurchaseTemporaryPlanMapper.selectNcPurchaseTemporaryPlanList(ncPurchaseTemporaryPlan);
    }

    @Override
    public List<NcPurchaseTemporaryPlan> selectNcPurchaseTemporaryPlanBySupplierCode(NcPurchaseTemporaryPlan ncPurchaseTemporaryPlan) {
        return ncPurchaseTemporaryPlanMapper.selectNcPurchaseTemporaryPlanBySupplierCode(ncPurchaseTemporaryPlan);
    }

    /**
     * 新增采购临时计划
     *
     * @param ncPurchaseTemporaryPlan 采购临时计划
     * @return 结果
     */
    @Override
    public int insertNcPurchaseTemporaryPlan(NcPurchaseTemporaryPlan ncPurchaseTemporaryPlan)
    {
        List<JcIteminfoPo> jcIteminfoList = ncPurchaseTemporaryPlan.getJcIteminfoList();
        for(JcIteminfoPo jcIteminfo : jcIteminfoList){
            ncPurchaseTemporaryPlan.setMaterialType(jcIteminfo.getClassName());
            ncPurchaseTemporaryPlan.setMaterialNumber(jcIteminfo.getItemCode());
            ncPurchaseTemporaryPlan.setMaterialSubnumber(jcIteminfo.getItemId());
            ncPurchaseTemporaryPlan.setMaterialName(jcIteminfo.getItemName());
            ncPurchaseTemporaryPlan.setSpecification(jcIteminfo.getSpec());
            ncPurchaseTemporaryPlan.setMaterialUnit(jcIteminfo.getUnit());
            ncPurchaseTemporaryPlan.setBarcode(jcIteminfo.getTmh());
            NcSupplier ncSupplier = ncSupplierMapper.selectNcSupplierById(jcIteminfo.getSupplyId());
            if(ncSupplier!=null){
                ncPurchaseTemporaryPlan.setSupplierCode(ncSupplier.getSupplierCode());
                ncPurchaseTemporaryPlan.setSupplierName(ncSupplier.getSupplierName());

            }
            ncPurchaseTemporaryPlan.setCustodyAttribute(jcIteminfo.getIsTuoguan());
            ncPurchaseTemporaryPlan.setTemporaryPlanQuantity(jcIteminfo.getYhNum());
            ncPurchaseTemporaryPlan.setPlannedPrice(jcIteminfo.getJhUnitPrice());
            ncPurchaseTemporaryPlan.setPlannedAmount(jcIteminfo.getJhTotalPrice());
            ncPurchaseTemporaryPlan.setInclusivePurchasePrice(jcIteminfo.getInclusivePurchasePrice());
            ncPurchaseTemporaryPlan.setInclusivePurchaseAmount(jcIteminfo.getInclusivePurchaseAmount());
            ncPurchaseTemporaryPlan.setExclusivePurchasePrice(jcIteminfo.getExclusivePurchasePrice());
            ncPurchaseTemporaryPlan.setExclusivePurchaseAmount(jcIteminfo.getExclusivePurchaseAmount());
            ncPurchaseTemporaryPlan.setPackagingCost(jcIteminfo.getPackagingCost());
            ncPurchaseTemporaryPlan.setShippingCost(jcIteminfo.getShippingCost());
            ncPurchaseTemporaryPlan.setLogisticsCost(jcIteminfo.getLogisticsCost());
            ncPurchaseTemporaryPlan.setMaterial(jcIteminfo.getMaterial());
            ncPurchaseTemporaryPlan.setPackagingMaterial(jcIteminfo.getPackagingMaterial());
            ncPurchaseTemporaryPlan.setPackagingSize(jcIteminfo.getPackagingSize());
            ncPurchaseTemporaryPlan.setSingleBoxWeight(jcIteminfo.getSingleWeight());

            ncPurchaseTemporaryPlan.setCreateTime(DateUtils.getNowDate());
            ncPurchaseTemporaryPlanMapper.insertNcPurchaseTemporaryPlan(ncPurchaseTemporaryPlan);
        }
        return 1;

    }

    /**
     * 修改采购临时计划
     *
     * @param ncPurchaseTemporaryPlan 采购临时计划
     * @return 结果
     */
    @Override
    public int updateNcPurchaseTemporaryPlan(NcPurchaseTemporaryPlan ncPurchaseTemporaryPlan)
    {
        ncPurchaseTemporaryPlan.setUpdateTime(DateUtils.getNowDate());
        return ncPurchaseTemporaryPlanMapper.updateNcPurchaseTemporaryPlan(ncPurchaseTemporaryPlan);
    }

    /**
     * 批量删除采购临时计划
     *
     * @param ids 需要删除的采购临时计划主键
     * @return 结果
     */
    @Override
    public int deleteNcPurchaseTemporaryPlanByIds(Long[] ids)
    {
        return ncPurchaseTemporaryPlanMapper.deleteNcPurchaseTemporaryPlanByIds(ids);
    }

    /**
     * 删除采购临时计划信息
     *
     * @param id 采购临时计划主键
     * @return 结果
     */
    @Override
    public int deleteNcPurchaseTemporaryPlanById(Long id)
    {
        return ncPurchaseTemporaryPlanMapper.deleteNcPurchaseTemporaryPlanById(id);
    }
    @Override
    public String imporIteminfo(List<NcPurchaseTemporaryPlan> ncPurchaseTemporaryPlanList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ncPurchaseTemporaryPlanList) || ncPurchaseTemporaryPlanList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcPurchaseTemporaryPlan ncPurchaseTemporaryPlan : ncPurchaseTemporaryPlanList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ncPurchaseTemporaryPlan);
                ncPurchaseTemporaryPlan.setCreateBy(operName);
                ncPurchaseTemporaryPlan.setUpdateBy(operName);
                this.insertNcPurchaseTemporaryPlan(ncPurchaseTemporaryPlan);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<NcPurchaseTemporaryPlan> getNcPurchaseTemporaryPlanByPlanNumber(NcPurchaseTemporaryPlan ncPurchaseTemporaryPlan) {
        return ncPurchaseTemporaryPlanMapper.getNcPurchaseTemporaryPlanByPlanNumber(ncPurchaseTemporaryPlan);
    }
}
